package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.ReturnOrderScanConverter;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.enums.VehicleBringScanTypeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.warehouse.ReturnOrderScanMapper;
import com.quad.innovators.salesease.model.entity.warehouse.ReturnOrderScan;
import com.quad.innovators.salesease.model.entity.warehouse.VehicleBringGoods;
import com.quad.innovators.salesease.model.entity.warehouse.VehicleBringScanCode;
import com.quad.innovators.salesease.model.form.warehouse.ReturnOrderScanForm;
import com.quad.innovators.salesease.model.vo.warehouse.SaleReturnScanNumberVO;
import com.quad.innovators.salesease.model.vo.warehouse.ScanCodeVO;
import com.quad.innovators.salesease.service.warehouse.ReturnOrderScanService;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/26 10:30
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class ReturnOrderScanServiceImpl extends ServiceImpl<ReturnOrderScanMapper, ReturnOrderScan> implements ReturnOrderScanService {

    private final ReturnOrderScanConverter returnOrderScanConverter;

    @Override
    public List<ScanCodeVO> getScanDetail(String returnOrderDetailCode) {
        return this.baseMapper.getScanDetail(returnOrderDetailCode);
    }

    @Override
    public List<ScanCodeVO> getScanDetailBySaleReturnCode(String code) {
        return this.baseMapper.getScanDetailBySaleReturnCode(code);
    }

    @Override
    public List<ReturnOrderScanForm> getListByVehicleCode(String returnOrderScanCode) {
        List<ReturnOrderScan> returnOrderScanList = this.list(new QueryWrapper<ReturnOrderScan>().lambda().eq(ReturnOrderScan::getReturnOrderDetailCode, returnOrderScanCode));
        return returnOrderScanList.stream().map(returnOrderScanConverter::toForm).toList();
    }

    @Override
    public boolean delete(String code) {
        ReturnOrderScan returnOrderScan = this.getOne(new QueryWrapper<ReturnOrderScan>().lambda().eq(ReturnOrderScan::getCode, code));
        if (ObjectUtils.isEmpty(returnOrderScan)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return this.removeById(returnOrderScan.getId());
    }

    @Override
    public String save(ReturnOrderScanForm form) {
        ReturnOrderScan entity = returnOrderScanConverter.toEntity(form);
        entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.RETURN_ORDER_SCAN));
        boolean save = this.save(entity);
        return save ? entity.getCode() : "";
    }

    @Override
    public List<SaleReturnScanNumberVO> getReturnNumberInDetailCodeList(List<String> stringList) {
        return this.baseMapper.getReturnNumberInDetailCodeList(stringList);
    }
}
